
#include "stdafx.h"

//////////////////////////// WSACleanup ////////////////////////////
//TEST(WSACleanupTest, WSACleanup_in_inout)
//{
//    
//}

//TEST(WSACleanupTest, WSACleanup_in_inout_NULL_parameters)
//{
//    
//}

TEST(WSACleanupTest, WSACleanup_out_inout)
{
    SOCKET sock = INVALID_SOCKET;
    
    Header header(GetCurrentThreadId(), "WSACleanup");
    WSACleanup_out_inout WSACleanupIN(7);
    WSACleanup_out_inout WSACleanupOUT;

    std::vector<char> buffer;
    Serialize(header, WSACleanupIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(WSACleanupOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (WSACleanupIN.wsa_last_error_, WSACleanupOUT.wsa_last_error_);
}

//TEST(WSACleanupTest, WSACleanup_out_inout_NULL_parameters)
//{
//    
//}

//////////////////////////// __WSAFDIsSet ////////////////////////////
TEST(__WSAFDIsSetTest, __WSAFDIsSet_in_inout)
{
    SOCKET sock = INVALID_SOCKET;
    fd_set fdset;
    fdset.fd_count = 3;
    fdset.fd_array[0] = 1;
    fdset.fd_array[1] = 2;
    fdset.fd_array[2] = 3;
    
    Header header(GetCurrentThreadId(), "__WSAFDIsSet");
    __WSAFDIsSet_in_inout __WSAFDIsSetIN(sock, &fdset);
    __WSAFDIsSet_in_inout __WSAFDIsSetOUT;

    std::vector<char> buffer;
    Serialize(header, __WSAFDIsSetIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(__WSAFDIsSetOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (__WSAFDIsSetIN.fd_, __WSAFDIsSetOUT.fd_);
    if (__WSAFDIsSetIN.set_ && __WSAFDIsSetOUT.set_)
    {
        EXPECT_EQ (__WSAFDIsSetIN.set_->fd_count, __WSAFDIsSetOUT.set_->fd_count);
        EXPECT_LE (__WSAFDIsSetIN.set_->fd_count, (u_int)FD_SETSIZE);
        int cmp = memcmp (__WSAFDIsSetIN.set_->fd_array, __WSAFDIsSetOUT.set_->fd_array, 
                          __WSAFDIsSetIN.set_->fd_count * sizeof(SOCKET));
        EXPECT_TRUE (!cmp);
        /*for (u_int i=0; i<__WSAFDIsSetIN.set_->fd_count && i<FD_SETSIZE; ++i)
        {
            EXPECT_EQ (__WSAFDIsSetIN.set_->fd_array[i],
                       __WSAFDIsSetOUT.set_->fd_array[i]);
        }*/
    }
    else {
        EXPECT_FALSE(__WSAFDIsSetIN.set_);
        EXPECT_FALSE(__WSAFDIsSetOUT.set_);
    }
}

TEST(__WSAFDIsSetTest, __WSAFDIsSet_in_inout_NULL_parameters)
{
    SOCKET sock = INVALID_SOCKET;
    fd_set fdset;
    fdset.fd_count = 3;
    fdset.fd_array[0] = 1;
    fdset.fd_array[1] = 2;
    fdset.fd_array[2] = 3;
    
    Header header(GetCurrentThreadId(), "__WSAFDIsSet");
    __WSAFDIsSet_in_inout __WSAFDIsSetIN(sock, NULL);
    __WSAFDIsSet_in_inout __WSAFDIsSetOUT;

    std::vector<char> buffer;
    Serialize(header, __WSAFDIsSetIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(__WSAFDIsSetOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (__WSAFDIsSetIN.fd_, __WSAFDIsSetOUT.fd_);
    if (__WSAFDIsSetIN.set_ && __WSAFDIsSetOUT.set_)
    {
        EXPECT_EQ (__WSAFDIsSetIN.set_->fd_count, __WSAFDIsSetOUT.set_->fd_count);
        EXPECT_LE (__WSAFDIsSetIN.set_->fd_count, (u_int)FD_SETSIZE);
        int cmp = memcmp (__WSAFDIsSetIN.set_->fd_array, __WSAFDIsSetOUT.set_->fd_array, 
                          __WSAFDIsSetIN.set_->fd_count * sizeof(SOCKET));
        EXPECT_TRUE (!cmp);
    }
    else {
        EXPECT_FALSE(__WSAFDIsSetIN.set_);
        EXPECT_FALSE(__WSAFDIsSetOUT.set_);
    }
}

TEST(__WSAFDIsSetTest, __WSAFDIsSet_out_inout)
{
    SOCKET sock = INVALID_SOCKET;
    
    Header header(GetCurrentThreadId(), "__WSAFDIsSet");
    __WSAFDIsSet_out_inout __WSAFDIsSetIN(1, 7);
    __WSAFDIsSet_out_inout __WSAFDIsSetOUT;

    std::vector<char> buffer;
    Serialize(header, __WSAFDIsSetIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(__WSAFDIsSetOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (__WSAFDIsSetIN.ret_, __WSAFDIsSetOUT.ret_);
    EXPECT_EQ (__WSAFDIsSetIN.wsa_last_error_, __WSAFDIsSetOUT.wsa_last_error_);
}

//TEST(__WSAFDIsSetTest, __WSAFDIsSet_out_inout_NULL_parameters)
//{
//    
//}

//////////////////////////// WSAGetLastError ////////////////////////////
//TEST(WSAGetLastErrorTest, WSAGetLastError_in_inout)
//{
//    
//}

//TEST(WSAGetLastErrorTest, WSAGetLastError_in_inout_NULL_parameters)
//{
//    
//}

TEST(WSAGetLastErrorTest, WSAGetLastError_out_inout)
{
    SOCKET sock = INVALID_SOCKET;
    
    Header header(GetCurrentThreadId(), "WSAGetLastError");
    WSAGetLastError_out_inout WSAGetLastErrorIN(7);
    WSAGetLastError_out_inout WSAGetLastErrorOUT;

    std::vector<char> buffer;
    Serialize(header, WSAGetLastErrorIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(WSAGetLastErrorOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (WSAGetLastErrorIN.ret_, WSAGetLastErrorOUT.ret_);
}

//TEST(WSAGetLastErrorTest, WSAGetLastError_out_inout_NULL_parameters)
//{
//    
//}

//////////////////////////// WSASetLastError ////////////////////////////
// as WSAGetLastError

//////////////////////////// WSAStartup ////////////////////////////
TEST(WSAStartupTest, WSAStartup_in_inout)
{
    SOCKET sock = INVALID_SOCKET;
    
    Header header(GetCurrentThreadId(), "WSAStartup");
    WSAStartup_in_inout WSAStartupIN( MAKEWORD(2,2) );
    WSAStartup_in_inout WSAStartupOUT;

    std::vector<char> buffer;
    Serialize(header, WSAStartupIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(WSAStartupOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (WSAStartupIN.wVersionRequested_, WSAStartupOUT.wVersionRequested_);
}

//TEST(WSAStartupTest, WSAStartup_in_inout_NULL_parameters)
//{
//    
//}

TEST(WSAStartupTest, WSAStartup_out_inout)
{
    WSADATA wsaData;

    WSAStartup( MAKEWORD(2,2), &wsaData );
    
    Header header(GetCurrentThreadId(), "WSAStartup");
    WSAStartup_out_inout WSAStartupIN( 11, wsaData );
    WSAStartup_out_inout WSAStartupOUT;

    std::vector<char> buffer;
    Serialize(header, WSAStartupIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(WSAStartupOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (WSAStartupIN.ret_, WSAStartupOUT.ret_);
    EXPECT_EQ (WSAStartupIN.WSAData_.wVersion, WSAStartupOUT.WSAData_.wVersion);
    EXPECT_EQ (WSAStartupIN.WSAData_.wHighVersion, WSAStartupOUT.WSAData_.wHighVersion);
    EXPECT_TRUE (!strncmp (WSAStartupIN.WSAData_.szDescription,
                           WSAStartupOUT.WSAData_.szDescription, 
                           WSADESCRIPTION_LEN));
    EXPECT_TRUE (!strncmp (WSAStartupIN.WSAData_.szSystemStatus,
                           WSAStartupOUT.WSAData_.szSystemStatus, 
                           WSASYS_STATUS_LEN));
    EXPECT_EQ (WSAStartupIN.WSAData_.iMaxSockets, WSAStartupOUT.WSAData_.iMaxSockets);
    EXPECT_EQ (WSAStartupIN.WSAData_.iMaxUdpDg, WSAStartupOUT.WSAData_.iMaxUdpDg);
    /*EXPECT_TRUE (!strcmp (WSAStartupIN.WSAData_.lpVendorInfo,
                          WSAStartupOUT.WSAData_.lpVendorInfo));*/
}

//TEST(WSAStartupTest, WSAStartup_out_inout_NULL_parameters)
//{
//    
//}

